﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace DoNet.Common.Helpers
{
    public class ConfigHelper
    {
        /// <summary>
        /// 
        /// </summary>
        public static IServiceCollection? services;
        /// <summary>
        /// 
        /// </summary>
        public static IConfiguration? configuration;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_configuration"></param>
        public ConfigHelper(IConfiguration _configuration)
        {
            configuration = _configuration;
        }
        /// <summary>
        /// 获取对应的配置
        /// ConfigService.GetSection("OpenGlobal")
        /// ConfigService.GetSection("AppSetting:OpenGlobal")
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public static IConfigurationSection? GetSection(string section)
        {
            return configuration?.GetSection(section);
        }
        /// <summary>
        /// 获取对应的配置的值
        /// ConfigService.GetSectionValue("OpenGlobal")
        /// ConfigService.GetSectionValue("AppSetting:OpenGlobal")
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public static string GetSectionValue(string section)
        {
            return GetSection(section)?.Value ?? string.Empty;
        }
        /// <summary>
        /// 获取对应的配置的值
        /// ConfigService.GetSectionValue("OpenGlobal")
        /// ConfigService.GetSectionValue(new string[] { "AppSetting", "OpenGlobal" })
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static string GetSectionValue(params string[] sections)
        {
            try
            {
                if (configuration != null)
                {
                    if (sections.Any())
                    {
                        return configuration[string.Join(":", sections)] ?? string.Empty;
                    }
                }
            }
            catch (Exception) { }

            return string.Empty;
        }

        /// <summary>
        /// 获取对应的配置的key的值
        /// ConfigService.GetConfigurationValue("SqlAOP", "Enabled")
        /// ConfigService.GetConfigurationValue("AppSetting:SqlAOP", "Enabled")
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigurationValue(string section, string key)
        {
            return GetSection(section)?[key] ?? string.Empty;
        }

        /// <summary>
        /// 获取对应的配置的实体
        /// ConfigService.GetSection<DbConnections>("ConnectionStrings")
        /// ConfigService.GetSection<DbConnections>("AppSetting:ConnectionStrings")
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="section"></param>
        /// <returns></returns>
        public static T? GetSection<T>(string section)
        {
            var configurationSection = GetSection(section);
            if (configurationSection != null)
                return configurationSection.Get<T>();
            else
                return default;
        }

        /// <summary>
        /// 获取对应的配置的实体列表
        /// ConfigService.GetSection<DbConnections>("ConnectionStrings")
        /// ConfigService.GetSection<DbConnections>(new string[] { "AppSetting", "ConnectionStrings" })
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static List<T> GetSection<T>(params string[] sections)
        {
            List<T> list = new List<T>();
            // 引用 Microsoft.Extensions.Configuration.Binder 包
            configuration?.Bind(string.Join(":", sections), list);
            return list;
        }

        /// <summary>
        /// 根据Key获取数据库连接字符串（只适用于一个数据库连接配置）
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnectionString(string key)
        {
            //ConnectionStrings
            //name
            //connectionString
            //providerName
            return configuration?.GetConnectionString(key) ?? string.Empty;
        }
    }
}
